package com.example.quickadapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import androidx.recyclerview.widget.RecyclerView
import kotlin.properties.Delegates


abstract class BaseQuickAdapter<T,U>(layout:Int):RecyclerView.Adapter<BaseViewHolder>(){
    var layout by Delegates.notNull<Int>()

    init {
        this.layout=layout
    }

    var data=ArrayList<T>()
    @JvmField
    var onItemClickListener:OnItemClickListener?=null
    @JvmField
    var onItemLongClickListener: OnItemLongClickListener?=null
    @JvmField
    var onItemChildClickListener:OnItemChildClickListener?=null
    var ids=ArrayList<Int>()
    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        var manager = recyclerView.layoutManager
        if(manager is GridLayoutManager){
            manager=manager as GridLayoutManager
            manager.spanSizeLookup=object :SpanSizeLookup(){
                override fun getSpanSize(position: Int): Int {
                    return 1
                }

            }
        }
    }

    //abstract fun getMultiSpanSize(position: Int, recyclerView: RecyclerView): Int
    //添加子控件监听方法
    fun addItemChildClick(ids:List<Int>){
        for (id in ids){
            this.ids.add(id)
        }
    }

    //创建ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
//        val view = createItemView(viewType, parent)
        val view = LayoutInflater.from(parent.context).inflate(layout, parent, false)
        val viewHolder = BaseViewHolder(view)
        bindListener(viewHolder)
        return viewHolder
    }

    private fun bindListener(viewHolder: BaseViewHolder) {
        if(onItemClickListener!=null){
            viewHolder.itemView.setOnClickListener {
                onItemClickListener!!.onItemClick(viewHolder.adapterPosition)
            }
        }
        if(onItemLongClickListener!=null){

                viewHolder.itemView.setOnLongClickListener  {
                    onItemLongClickListener!!.onItemLongClick(viewHolder.adapterPosition)
                }
        }
        if(onItemChildClickListener!=null){
            for (id in ids){
                viewHolder.getView<View>(id).setOnClickListener {
                    onItemChildClickListener!!.onItemChildClick(id,viewHolder.adapterPosition)
                }
            }
        }
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener){
        this.onItemClickListener=onItemClickListener
    }

    fun setOnItemLongClickListener(onItemLongClickListener: OnItemLongClickListener){
        this.onItemLongClickListener=onItemLongClickListener
    }

    fun setOnItemchildClickListener(onItemChildClickListener: OnItemChildClickListener){
        this.onItemChildClickListener=onItemChildClickListener
    }

//    abstract fun createItemView(viewType: Int, parent: ViewGroup):View

    fun addData(entity: T){
        data.add(entity)
        //刷新最后一条数据
        notifyItemInserted(data.size-1)
    }

    fun addAll(list:List<T>){
        data.clear()
        data.addAll(list)
        notifyDataSetChanged()
    }

    fun getData():List<T>{
        return data
    }

    fun remove(position: Int){
        data.removeAt(position)
        notifyItemRemoved(position)
    }

    //赋值
    abstract fun convert(holder: BaseViewHolder,item:T)

    override fun getItemCount(): Int {
        return data.size
    }

    //绑定ViewHolder
    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val t = data.get(position) as T
        convert(holder,t)
    }

    //abstract override fun getItemViewType(position: Int): Int

    interface OnItemClickListener{
        fun onItemClick(position:Int)
    }

    interface OnItemLongClickListener{
        fun onItemLongClick(position:Int):Boolean
    }

    interface OnItemChildClickListener{
        fun onItemChildClick(id:Int,position:Int)
    }
}

